@extends('layouts.admin.application')
@section('content')

    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">优惠券</strong> /
                    <small>List</small>
                </div>
            </div>
            @include('admin._flash')
            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="/admin/coupon/create" type="button" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增</a>
                            <button type="button" class="am-btn am-btn-default delete_all"><span
                                        class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <hr>


            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr style="font-size: 1.6rem">

                                <th class="table-id">ID</th>
                                <th class="table-type">优惠折度</th>
                                <th class="table-author" style="width:70px;">排序</th>
                                <th class="table-author">适用范围</th>
                                <th class="table-title">是否启用</th>
                                <th class="table-author">有效期限</th>
                                <th class="table-author">所属门店</th>
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($coupons as  $k=> $c)
                                <tr data-id="{{$c->id}}" style="font-size: 1.6rem">

                                    <td>{{$k+1}}</td>
                                    <td>{{$c->ratio}}折</td>


                                    <td>
                                        <input type="text" class="stock sort_order" value="{{$c->sort}}">
                                    </td>

                                    <td>
                                        @foreach($c->types as $t)
                                            <label class="am-btn am-btn-danger am-btn-xs am-radius">
                                                {{$t->name}}
                                            </label>

                                        @endforeach
                                    </td>


                                    <td>
                                        @if($c->is_use == 1)
                                            <span class="am-icon-check change_attr"></span>
                                        @endif
                                        @if($c->is_use == 0)
                                            <span class="am-icon-close change_attr"></span>
                                        @endif
                                    </td>
                                    <td>
                                        {{$c->start_at}}至{{$c->end_at}}
                                    </td>
                                    <td>
                                        <label class="am-btn am-btn-danger am-btn-xs am-radius">
                                            {{$c['store']['name']}}
                                        </label>
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="/admin/coupon/{{$c->id}}/edit"
                                                   class="am-btn am-btn-primary am-btn-xs"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <a href="/admin/coupon/{{$c->id}}"
                                                   data-method="delete"
                                                   data-token="{{csrf_token()}}" data-confirm="确认删除吗?"
                                                   class="am-btn am-btn-danger am-btn-xs del_one">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach


                            </tbody>
                        </table>
                        共 {{$coupons->total()}} 条记录

                        <div class="am-cf">
                            <div class="am-fr">
                                {!! $coupons->links() !!}
                            </div>
                        </div>
                        <hr/>
                        <p>注：.....</p>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>


@endsection
@section('js')
    <script>
        $(function () {


            //ajax排列
            $('.sort_order').change(function () {
                var sort = $(this).val();
                var id = $(this).parents('tr').data('id');
//                console.log(id);return false;
                $.ajax({
                    type: 'PATCH',
                    url: '/admin/coupon/sort',
                    data: {sort: sort, id: id},
                    success: function () {
                        location.href = "/admin/coupon"
                    }
                })
            })

            //改变属性
            $('.change_attr').click(function () {
                //   alert(111);

                var id = $(this).parents('tr').data('id');
// console.log(id);exit;
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '/admin/coupon/change_attr',
                    data: {id: id},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                })

            })


        })
    </script>
@endsection